home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / • Extras • / SGI STL / set.h < prev    next >
C/C++ Source or Header  |  1997-05-28  |  8KB  |  232 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  */
  26.  
  27. /*
  28.  *
  29.  * Copyright (c) 1997
  30.  * Moscow Center for SPARC Technology
  31.  *
  32.  * Permission to use, copy, modify, distribute and sell this software
  33.  * and its documentation for any purpose is hereby granted without fee,
  34.  * provided that the above copyright notice appear in all copies and
  35.  * that both that copyright notice and this permission notice appear
  36.  * in supporting documentation.  Moscow Center for SPARC Technology makes no
  37.  * representations about the suitability of this software for any
  38.  * purpose.  It is provided "as is" without express or implied warranty.
  39.  *
  40.  */
  41.  
  42. #ifndef __SGI_STL_SET_H
  43. #define __SGI_STL_SET_H
  44.  
  45. # ifndef __SGI_STL_TREE_H
  46. #  include <tree.h>
  47. # endif
  48.  
  49. __BEGIN_STL_NAMESPACE
  50. __BEGIN_STL_FULL_NAMESPACE
  51. #define set __WORKAROUND_RENAME(set)
  52.  
  53. template <class Key, __DFL_TMPL_PARAM(Compare,less<Key>), 
  54.                      __DFL_TYPE_PARAM(Alloc,alloc) >
  55. class set {
  56.     typedef set<Key, Compare, Alloc> self;
  57. public:
  58. // typedefs:
  59.     typedef Key key_type;
  60.     typedef Key value_type;
  61.     typedef Compare key_compare;
  62.     typedef Compare value_compare;
  63.     typedef rb_tree<key_type, value_type, 
  64.                     identity<value_type>, key_compare, Alloc> rep_type;
  65.     typedef typename rep_type::const_pointer   pointer;
  66.     typedef typename rep_type::const_reference reference;
  67.     typedef typename rep_type::const_reference const_reference;
  68.     typedef typename rep_type::const_iterator  const_iterator;
  69.     // SunPro 4.1, 4.0.1 bug
  70. #  ifdef __SUNPRO_CC   
  71.     typedef const_iterator iterator;
  72. #  else
  73.     typedef typename rep_type::const_iterator iterator;
  74. #  endif
  75.     typedef typename rep_type::const_reverse_iterator reverse_iterator;
  76.     typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
  77.     typedef typename rep_type::size_type size_type;
  78.     typedef typename rep_type::difference_type difference_type;
  79. private:
  80.     rep_type t;  // red-black tree representing set
  81. public:
  82. // allocation/deallocation
  83.  
  84.     set() : t(Compare()) {}
  85.     explicit set(const Compare& comp) : t(comp) {}
  86.     set(const value_type* first, const value_type* last) : t(Compare()) {
  87.           t.insert_unique(first, last);
  88.     }
  89.     set(const value_type* first, const value_type* last, 
  90.              const Compare& comp) : t(comp) {
  91.           t.insert_unique(first, last);
  92.     }
  93.     set(const_iterator first, const_iterator last ) : t(Compare()) {
  94.                t.insert_unique(first, last);
  95.     }
  96.     set(const_iterator first, const_iterator last, 
  97.         const Compare& comp) : t(comp) {
  98.                t.insert_unique(first, last);
  99.     }
  100.     set(const self& x) : t(x.t) {}
  101.     self& operator=(const self& x) { 
  102.         t = x.t; 
  103.         return *this;
  104.     }
  105.  
  106. // accessors:
  107.  
  108.     key_compare key_comp() const { return t.key_comp(); }
  109.     value_compare value_comp() const { return t.key_comp(); }
  110.     iterator begin() const { return t.begin(); }
  111.     iterator end() const { return t.end(); }
  112.     reverse_iterator rbegin() const { return t.rbegin(); } 
  113.     reverse_iterator rend() const { return t.rend(); }
  114.     bool empty() const { return t.empty(); }
  115.     size_type size() const { return t.size(); }
  116.     size_type max_size() const { return t.max_size(); }
  117.     void swap(self& x) { t.swap(x.t); }
  118.  
  119. // insert/erase
  120.     typedef  pair<iterator, bool> pair_iterator_bool; 
  121.     pair<iterator,bool> insert(const value_type& x) { 
  122.         pair<typename rep_type::iterator, bool> p = t.insert_unique(x); 
  123.         return pair<iterator, bool>(p.first, p.second);
  124.     }
  125.     iterator insert(iterator position, const value_type& x) {
  126.         return t.insert_unique((typename rep_type::iterator&)position, x);
  127.     }
  128.     void insert(const_iterator first, const_iterator last) {
  129.       t.insert_unique(first, last);
  130.     }
  131.     void insert(const value_type* first, const value_type* last) {
  132.       t.insert_unique(first, last);
  133.     }
  134.     void erase(iterator position) { 
  135.         t.erase((typename rep_type::iterator&)position); 
  136.     }
  137.     size_type erase(const key_type& x) { 
  138.         return t.erase(x); 
  139.     }
  140.     void erase(iterator first, iterator last) { 
  141.         t.erase((typename rep_type::iterator&)first, 
  142.                 (typename rep_type::iterator&)last); 
  143.     }
  144.     void clear() { t.clear(); }
  145.  
  146. // set operations:
  147.  
  148.     iterator find(const key_type& x) const { return t.find(x); }
  149.     size_type count(const key_type& x) const { return t.count(x); }
  150.     iterator lower_bound(const key_type& x) const {
  151.         return t.lower_bound(x);
  152.     }
  153.     iterator upper_bound(const key_type& x) const {
  154.         return t.upper_bound(x); 
  155.     }
  156.     pair<iterator,iterator> equal_range(const key_type& x) const {
  157.         return t.equal_range(x);
  158.     }
  159.   friend bool operator==(const self&, const self&);
  160.   friend bool operator<(const self&, const self&);
  161. };
  162.  
  163. __END_STL_FULL_NAMESPACE
  164.  
  165. // do a cleanup
  166. # undef set
  167. // provide a way to access full funclionality 
  168. # define __set__  __FULL_NAME(set)
  169.  
  170. template <class Key, class Compare, class Alloc>
  171. inline bool operator==(const __set__<Key, Compare, Alloc>& x, 
  172.                        const __set__<Key, Compare, Alloc>& y) {
  173.   return operator==(x.t,y.t);
  174. }
  175.  
  176. template <class Key, class Compare, class Alloc>
  177. inline bool operator<(const __set__<Key, Compare, Alloc>& x, 
  178.                       const __set__<Key, Compare, Alloc>& y) {
  179.   return x.t < y.t ;
  180. }
  181.  
  182. # if defined (__STL_CLASS_PARTIAL_SPECIALIZATION )
  183. template <class Key, class Compare, class Alloc>
  184. inline void swap(__set__<Key, Compare, Alloc>& a,
  185.                  __set__<Key, Compare, Alloc>& b) { a.swap(b); }
  186. # endif
  187.  
  188. # ifndef __STL_DEFAULT_TYPE_PARAM
  189. // provide a "default" set adaptor
  190. template <class Key, class Compare>
  191. class set : public __set__<Key, Compare, alloc>
  192. {
  193.     typedef set<Key, Compare> self;
  194. public:
  195.     typedef __set__<Key, Compare, alloc> super;
  196.     __CONTAINER_SUPER_TYPEDEFS
  197.     // copy & assignment from super
  198.     __IMPORT_SUPER_COPY_ASSIGNMENT(set)
  199.     // specific constructors
  200.     explicit set() : super(Compare()) {}
  201.     explicit set(const Compare& comp) : super(comp) {}
  202.     set(const value_type* first, const value_type* last) : 
  203.         super(first, last, Compare()) { }
  204.     set(const value_type* first, const value_type* last, 
  205.         const Compare& comp) : super(first, last, comp) { }
  206.     set(const_iterator first, const_iterator last) : 
  207.         super(first, last, Compare()) { }
  208.     set(const_iterator first, const_iterator last, 
  209.         const Compare& comp) : super(first, last, comp) { }
  210. };
  211.  
  212. #  if defined (__STL_BASE_MATCH_BUG)
  213. template <class Key, class Compare>
  214. inline bool operator==(const set<Key, Compare>& x, 
  215.                        const set<Key, Compare>& y) {
  216.   typedef  set<Key, Compare>::super super;
  217.   return operator==((const super&)x,(const super&)y);
  218. }
  219.  
  220. template <class Key, class Compare>
  221. inline bool operator<(const set<Key, Compare>& x, 
  222.                       const set<Key, Compare>& y) {
  223.   typedef  set<Key, Compare>::super super;
  224.   return operator < ((const super&)x , (const super&)y);
  225. }
  226. #  endif
  227. # endif /*  __STL_DEFAULT_TEMPLATE_PARAM */
  228.  
  229. __END_STL_NAMESPACE
  230.  
  231. #endif
  232.